package topcbl.taxi;

import topcbl.task.SignInTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Splash extends Activity {
	Button btn_signin, btn_abort, btn_help;
	EditText et_acc, et_pass;
	TextView btn_signup;
	View title, sologan, body, tail;
	Animation animationTitle, animationSologan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		getResource();
		getAnimation();

		// ---
		title.startAnimation(animationTitle);
		sologan.startAnimation(animationSologan);
		btn_abort.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent move2Main = new Intent(Splash.this, MainActivity.class);
				startActivity(move2Main);
				overridePendingTransition(android.R.anim.slide_in_left,
						android.R.anim.slide_out_right);
			}
		});
		btn_signin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkInputInfo()) {
					String acc = et_acc.getText() + "";
					String pass = et_pass.getText() + "";
					new SignInTask(Splash.this, acc, pass).execute();
				}
			}
		});

		btn_signup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent register = new Intent(Splash.this,
						RegisterActivity.class);
				startActivity(register);
				overridePendingTransition(android.R.anim.slide_in_left,
						android.R.anim.slide_out_right);
			}
		});
		et_pass.setTransformationMethod(new PasswordTransformationMethod());
	}

	private void getAnimation() {
		animationTitle = AnimationUtils.loadAnimation(this,
				R.anim.animation_title);
		animationSologan = AnimationUtils.loadAnimation(this,
				R.anim.animation_sologan);
		animationTitle.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				body.setVisibility(View.VISIBLE);
				tail.setVisibility(View.VISIBLE);
			}
		});
	}

	private void getResource() {
		btn_signin = (Button) findViewById(R.id.btn_splash_signin);
		btn_abort = (Button) findViewById(R.id.btn_splash_abort);
		btn_help = (Button) findViewById(R.id.btn_splash_help);
		et_acc = (EditText) findViewById(R.id.splash_acc);
		et_pass = (EditText) findViewById(R.id.splash_pass);
		btn_signup = (TextView) findViewById(R.id.splash_link);
		title = (View) findViewById(R.id.splash_title);
		sologan = (View) findViewById(R.id.splash_sologan);
		body = (View) findViewById(R.id.splash_body);
		tail = (View) findViewById(R.id.splash_tail);
	}

	private boolean checkInputInfo() {
		if (et_acc.getText().length() > 0 && et_pass.getText().length() > 0) {
			return true;
		} else if (et_acc.getText().length() <= 0) {
			Toast.makeText(Splash.this,
					getString(R.string.splash_wrong_acc_input),
					Toast.LENGTH_SHORT).show();
			return false;
		} else if (et_pass.getText().length() <= 0) {
			Toast.makeText(Splash.this,
					getString(R.string.splash_wrong_pass_input),
					Toast.LENGTH_SHORT).show();
			return false;
		}
		return false;
	}

}
